﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing.Design;

namespace Jemgine.Data
{
    public interface TexturedObject
    {
        String Texture { get; set; }

        void SetTexture(String ID, Texture2D Texture);
    }

    public class TextureAlignmentConverter : ExpandableObjectConverter
    {
        public override bool CanConvertTo(ITypeDescriptorContext context, System.Type destinationType)
        {
            if (destinationType == typeof(TextureAlignment))
                return true;
            return base.CanConvertTo(context, destinationType);
        }

        public override object ConvertTo(ITypeDescriptorContext context,
                               System.Globalization.CultureInfo culture,
                               object value,
                               System.Type destinationType)
        {
            if (destinationType == typeof(System.String) &&
                 value is TextureAlignment)
            {
                var so = (TextureAlignment)value;
                return so.ToString();
            }
            return base.ConvertTo(context, culture, value, destinationType);
        }
    }

    [TypeConverter(typeof(TextureAlignmentConverter))]
    public class TextureAlignment
    {
        public Vector2 Offset { get; set; }
        public float Angle { get; set; }
        public Vector2 Scale { get; set; }

        public TextureAlignment()
        {
        }

        public override string ToString()
        {
            return Offset.ToString() + " " + Angle.ToString() + " " + Scale.ToString();
        }

        public Matrix GetMatrix()
        {
            return Matrix.CreateRotationZ(MathHelper.ToRadians(Angle))
                * Matrix.CreateScale(Scale.X, Scale.Y, 1.0f)
                * Matrix.CreateTranslation(Offset.X, Offset.Y, 0);
        }

        public TextureAlignment Clone()
        {
            var R = new TextureAlignment();
            R.Angle = Angle;
            R.Offset = Offset;
            R.Scale = Scale;
            return R;
        }
    }

    public interface AlignableTexture
    {
        TextureAlignment Alignment { get; set; }
    }
}
